package com.hbyj.struts.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.hbyj.db.function.DbTools;
import com.hbyj.hbm.vo.Members;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
	
	public void userLogin() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String hql = "from Members where name = '"+username+"' and password = '"+password+"'";
		List<Members> list = DbTools.findByHql(hql);
		if(list.size()==1){
			Members memb = list.get(0);
			request.getSession().setAttribute("SysUser",memb);
			ServletActionContext.getResponse().getOutputStream().write("true".getBytes("utf-8"));
		}else{
			ServletActionContext.getResponse().getOutputStream().write("false".getBytes("utf-8"));
		}
	}
	/**
	 * author cuiyi
	 * time 2012-11-15
	 * disc 用户退出
	 * @return 
	 */
	public String UserExit() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		Members mbInfo = (Members)session.getAttribute("SysUser");
		String userName = mbInfo.getName();
		session.removeAttribute("SysUser");
		session.removeAttribute(userName);
		return SUCCESS;
	}
}
